import 'package:flutter/material.dart';
import 'package:detect/widgets/empty_widget.dart';
import 'package:detect/widgets/loading_widget.dart';

class FutureWidget<T> extends StatelessWidget {
  final Future<T>? future;
  final String? emptyTitle;
  final String? emptySubTitle;
  final String? loadingDesc;
  final Color? loadingColor;
  final Color? loadingBackgroundColor;
  final AsyncWidgetBuilder<T>? doneBuilder;

  const FutureWidget({
    super.key,
    required this.future,
    this.emptyTitle = '暂无数据',
    this.emptySubTitle = '您还没有相关数据哦~',
    this.loadingDesc = '努力加载中',
    this.loadingColor = Colors.white,
    this.loadingBackgroundColor = Colors.black,
    this.doneBuilder,
  });

  @override
  Widget build(BuildContext context) => FutureBuilder<T>(
        future: future,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (doneBuilder == null) {
              if (snapshot.hasData && snapshot.requireData is Widget) {
                return snapshot.requireData as Widget;
              } else {
                return EmptyWidget(title: emptyTitle, subTitle: emptySubTitle);
              }
            } else {
              return doneBuilder!.call(context, snapshot);
            }
          } else {
            return LoadingWidget(desc: loadingDesc, color: loadingColor, backgroundColor: loadingBackgroundColor);
          }
        },
      );
}
